package com.boot.service.impl;

import com.boot.domain.Order;
import com.boot.mapper.OrderMapper;
import com.boot.service.AccountService;
import com.boot.service.OrderService;
import com.boot.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Description:
 *
 * @Autor: Rolin
 * datetime: 05-09-19:38
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private StorageService storageService ;

    @Resource
    private AccountService accountService;



    @Override
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = ArithmeticException.class)
    public void create(Order order) {
//        。创建完订单之后，库存减，账户金额增加,订单状态改为 1
        orderMapper.create(order);
        storageService.decrease(order.getProductId(),order.getCount());
//        此处演示异常，用分布式事务来解决，演示feign的超时异常
        int i = 10/ 0 ;
        accountService.decrease(order.getUserId(),order.getMoney());

        orderMapper.update(order.getUserId(),1);
    }


}
